package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Album;
import com.baizhi.springboot.entity.AlbumDTO;
import com.baizhi.springboot.entity.Banner;
import com.baizhi.springboot.service.AlbumService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AlbumController {
    @Autowired
    private AlbumService albumService;
    @RequestMapping("/getAudioNumberList")
    @ResponseBody
    public Map getAudioNumberList() {
        HashMap<Object, Object> map = new HashMap<>();
        List<AlbumDTO> list = albumService.getAudioNumberList();
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> numberList = new ArrayList<>();
        for (AlbumDTO albumDTO : list) {
            nameList.add(albumDTO.getName());
            numberList.add(albumDTO.getNumber());
        }
        map.put("name", nameList);
        map.put("number", numberList);
        return map;
    }
    @RequestMapping("/getAudioNumber")
    @ResponseBody
    public List<Map> getAudioNumber() {
        return albumService.getAudioNumber();
    }
    @RequestMapping("/selectAllAlbum")
    @ResponseBody
    public List<Album> selectAllAlbum() {
        return albumService.selectAllAlbum();
    }
    @RequestMapping("/selectAlbumByPage")
    @ResponseBody
    public Map selectAlbumByPage(Integer page, Integer limit) {
        return albumService.selectAlbumByPage(page,limit);
    }
    @RequestMapping("/uploadAlbum")
    @ResponseBody
    public Map  uploadAlbum(MultipartFile file, HttpServletRequest request) throws IOException {
        //String realPath = request.getServletContext().getRealPath("/image");
        file.transferTo(new File("D:/uploadFile/"+file.getOriginalFilename()));
        String image=file.getOriginalFilename();
        System.out.println(image);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("image",image);
        return map;
    }
    @RequestMapping("/insertAlbum")
    @ResponseBody
    public Map  insertAlbum(Album album) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();

        try {
            albumService.insertAlbum(album);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteAlbum")
    @ResponseBody
    public Map  deleteAlbum(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            albumService.deleteAlbum(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneAlbum")
    @ResponseBody
    public Map selectOneAlbum(Integer id, HttpSession session) {
        Album album= albumService.selectOneAlbum(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("album", album);
        return map;
    }

    @RequestMapping("/updateAlbum")
    @ResponseBody
    public Map updateAlbum(Album album) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            albumService.updateAlbum(album);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
